/**
 *
 * @author Osvaldo Rosado
 */
import java.util.ArrayList;

public class GetHWND {
    public int GetHWND(String ThreadID) {
        WinApi WinApi;
        WinApi = new WinApi();
        int AppHwnd = 0 ;
        int ita = 0;
        final ArrayList<String> AppHandle = new ArrayList<String>();
        //Lets find the Application Handle
        System.err.println("Finding HWND for Process "+ ThreadID);
        int runner = 0;
        while (AppHwnd == 0){
            AppHandle.clear();
            WinApi.User32.EnumWindows(new WinApi.User32.WNDENUMPROC() {
                public boolean callback(int hWnd, int userData) {
                    AppHandle.add("" + hWnd);
                    //System.err.println("Found Handle "+hWnd);
                    return true;
                }
            }, null);
            ita = 0;
            int found = 0;
            
            while ((ita!=AppHandle.size())&&(found==0)){
                //System.err.println("Checking Handle "+AppHandle.get(ita));
                if(Integer.parseInt(ThreadID)==WinApi.User32.GetWindowThreadProcessId(Integer.parseInt(AppHandle.get(ita)), null)){
                    //Some Special Operating Modes
                    if(Main.GMCHECK==true){
                        System.err.println("Using GM Special Detection Mode");
                        //FIXME: It is possible for this to intercept ANY GM application.
                        while(runner<500){
                            try {
                                Thread.sleep(500);//Wait for GM app to load..
                            } catch (InterruptedException ex) {
                                //TODO: Catch This...
                            }
                            int AppHwnd2 = WinApi.User32.FindWindowA("TApplication", null);
                            WinApi.User32.SetParent(AppHwnd2,(int)Main.getAppletHandle(Main.panel1));
                            AppHwnd2 = WinApi.User32.FindWindowA("TRunnerForm", null);
                            WinApi.User32.SetParent(AppHwnd2,(int)Main.getAppletHandle(Main.panel1));
                            WinApi.User32.SetWindowLongA(AppHwnd2,-16,268435456);
                            WinApi.User32.MoveWindow(AppHwnd2,0,0,640,480,false);
                            if(AppHwnd2!=0){
                                AppHwnd=AppHwnd2;
                            }else if (AppHwnd!=0){
                                //Job done.
                                runner=500;
                            }
                            runner=runner+1;
                        }
                        ita=AppHandle.size();
                        found=1;
                        
                        
                            
                    }else if (Main.INSANITY==true){
                        System.err.println("Using INSANITY Special Detection Mode");
                        int AppHwnd2=Integer.parseInt(AppHandle.get(ita));
                        WinApi.User32.SetParent(AppHwnd2,(int)Main.getAppletHandle(Main.panel1));
                        WinApi.User32.SetWindowLongA(AppHwnd2,-16,268435456);
                        WinApi.User32.MoveWindow(AppHwnd2,0,0,Main.panel1.WIDTH,Main.panel1.HEIGHT,false);
                        ita=ita+1;
                        //break;
                        
                    }else if (!Main.CUSTOMCLASS.equals("")){
                        System.err.println("Using Custom ClassID Special Detection Mode");
                        int AppHwnd2 = 0;
                        while(AppHwnd2==0){
                            AppHwnd2 = WinApi.User32.FindWindowA(Main.CUSTOMCLASS, null);
                        }
                        AppHwnd=AppHwnd2;
                        ita=AppHandle.size();
                        found=1;
                    }else{
                    //End Special Modes
                    AppHwnd=Integer.parseInt(AppHandle.get(ita));
                    ita=AppHandle.size();
                    found=1;}
                }else{
                    //System.err.println(Integer.parseInt(processpid)+"!="+User32.GetWindowThreadProcessId(Integer.parseInt(AppHandle.get(ita)), null));
                    ita=ita+1;
                }
            }

        }
        return AppHwnd;
    }
}
